﻿using System;
using System.Collections.Generic;
using System.Data;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class MobSkillTable
    {
        private static Logger _log = Logger.getLogger( typeof( MobSkillTable ).FullName );

        private static MobSkillTable _instance;

        private Dictionary<int, L1MobSkill> _mobskills = new Dictionary<int, L1MobSkill>();

        public static MobSkillTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new MobSkillTable();
            }
            return _instance;
        }

        private MobSkillTable()
        {/*TTOK*/
            int count = 0;
            int mobid = 0;
            int actNo = 0;
            String sqlstr = "SELECT mobid,count(*) as cnt FROM mobskill group by mobid";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    mobid = SqlUtils.toInt( item["mobid"] );
                    count = SqlUtils.toInt( item["cnt"] );

                    sqlstr = "SELECT * FROM mobskill where mobid = ?1 order by mobid,actNo";
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = mobid;

                    L1MobSkill mobskill = new L1MobSkill( count );
                    mobskill.set_mobid( mobid );
                    foreach ( DataRow item2 in wrapper.GetDataTable( cmd ).Rows )
                    {
                        actNo = SqlUtils.toInt( item2["actNo"] );
                        mobskill.setMobName( Convert.ToString( item2["mobname"] ) );
                        mobskill.setType( actNo, SqlUtils.toInt( item2["type"] ) );
                        mobskill.setTriggerRandom( actNo, SqlUtils.toInt( item2["TriRnd"] ) );
                        mobskill.setTriggerHp( actNo, SqlUtils.toInt( item2["TriHp"] ) );
                        mobskill.setTriggerCompanionHp( actNo, SqlUtils.toInt( item2["TriCompanionHp"] ) );
                        mobskill.setTriggerRange( actNo, SqlUtils.toInt( item2["TriRange"] ) );
                        mobskill.setTriggerCount( actNo, SqlUtils.toInt( item2["TriCount"] ) );
                        mobskill.setChangeTarget( actNo, SqlUtils.toInt( item2["ChangeTarget"] ) );
                        mobskill.setRange( actNo, SqlUtils.toInt( item2["Range"] ) );
                        mobskill.setAreaWidth( actNo, SqlUtils.toInt( item2["AreaWidth"] ) );
                        mobskill.setAreaHeight( actNo, SqlUtils.toInt( item2["AreaHeight"] ) );
                        mobskill.setLeverage( actNo, SqlUtils.toInt( item2["Leverage"] ) );
                        mobskill.setSkillId( actNo, SqlUtils.toInt( item2["SkillId"] ) );
                        mobskill.setGfxid( actNo, SqlUtils.toInt( item2["Gfxid"] ) );
                        mobskill.setActid( actNo, SqlUtils.toInt( item2["Actid"] ) );
                        mobskill.setSummon( actNo, SqlUtils.toInt( item2["SummonId"] ) );
                        mobskill.setSummonMin( actNo, SqlUtils.toInt( item2["SummonMin"] ) );
                        mobskill.setSummonMax( actNo, SqlUtils.toInt( item2["SummonMax"] ) );
                        mobskill.setPolyId( actNo, SqlUtils.toInt( item2["PolyId"] ) );
                    }
                    _mobskills[mobid] = mobskill;
                }
            }
        }

        public L1MobSkill getTemplate( int id )
        {/*TTOK*/
            L1MobSkill ret;
            _mobskills.TryGetValue( id, out ret );
            return ret;
        }
    }
}
